﻿using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System;
using System.Text;
using System.IO;
using System.Xml;
using NLog;

namespace Tab2Sql.Properties
{
    // This class allows you to handle specific events on the settings class:
    //  The SettingChanging event is raised before a setting's value is changed.
    //  The PropertyChanged event is raised after a setting's value is changed.
    //  The SettingsLoaded event is raised after the setting values are loaded.
    //  The SettingsSaving event is raised before the setting values are saved.
    public sealed partial class Settings
    {
        private void SettingChangingEventHandler(object sender, SettingChangingEventArgs e)
        {
            // Add code to handle the SettingChangingEvent event here.
            Tab2Sql.Properties.Settings.Default.Save();

        }

        private void SettingsSavingEventHandler(object sender, CancelEventArgs e)
        {
            // Add code to handle the SettingsSaving event here.
        }
    }
}

namespace Tab2Sql
{
    /// <summary>
    /// Utility class for dealing with Configuration / Settings. This is a complete mess.
    /// </summary>
    public class SettingsHelper
    {
		private static Logger logger = LogManager.GetCurrentClassLogger ();
        private SettingsHelper ()
        {
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/>
        /// </returns>
        public static string ListSettings ()
        {

            string tmp = "";
        	string tmp2 = "";
        	tmp = DGen.Reflection.SettingsHelper.GetDefaultExeConfigPath ();
        	logger.Info ("Settings File given as {0}", tmp);
        	if (tmp.Length == 0)
			{
        		//var x = System.Configuration.LocalFileSettingsProvider()
        		tmp = "/home/dpl/tab2sql/tab2sql/Properties/Settings2.settings";
        	}
   
            if (File.Exists (tmp)) {
        		tmp2 = DGen.DFile.GetXmlFileAsString (tmp);
        	} else {
        		tmp2 = string.Format ("It does NOT exist {0}", tmp);
        	}
			
            
            return tmp2;

        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="ObjectToSerialize">
        /// A <see cref="System.Object"/>
        /// </param>
        /// <returns>
        /// A <see cref="System.String"/>
        /// </returns>
        public static string SerializeToXMLString(object ObjectToSerialize) {
            MemoryStream mem = new MemoryStream(); 
            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(ObjectToSerialize.GetType());
            ser.Serialize(mem,ObjectToSerialize);
            ASCIIEncoding ascii = new ASCIIEncoding(); 
            return ascii.GetString(mem.ToArray());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="TypeToDeserialize">
        /// A <see cref="Type"/>
        /// </param>
        /// <param name="xmlString">
        /// A <see cref="System.String"/>
        /// </param>
        /// <returns>
        /// A <see cref="System.Object"/>
        /// </returns>
        public static object DeSerializeFromXMLString(Type TypeToDeserialize, string xmlString)
        {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(xmlString);
            MemoryStream mem = new MemoryStream(bytes);
            System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(TypeToDeserialize);
            return ser.Deserialize(mem);
        }

    }
}